package com.v1.video.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.domain.CarouselInfo4;
import com.v1.video.domain.LoginInfo;
import com.v1.video.engine.NetEngine;
import com.v1.video.engine.RequestEngine;
import com.v1.video.util.Helper;
import com.v1.video.util.TransferUtils;

import java.util.ArrayList;
import java.util.List;

public class AdImageAdapter extends BaseAdapter {

	private Context mContext;
	private List<CarouselInfo4> mLstData;

	public AdImageAdapter(Context context) {
		mContext = context;
		mLstData = new ArrayList<CarouselInfo4>();
	}

	public List<CarouselInfo4> getLstData() {
		return mLstData;
	}

	public void setLstData(List<CarouselInfo4> mLst) {
		if (mLst != null && mLst.size() > 0) {
			this.mLstData = mLst;
			this.notifyDataSetChanged();
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (mLstData.size() > 1)
			return Integer.MAX_VALUE;
		else
			return mLstData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mLstData.get(position % mLstData.size());
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Holder holder = null;
		if (convertView == null) {
			convertView = View.inflate(mContext,
					R.layout.fragment_news_carousel_item, null);
			holder = new Holder();
			holder.imgView = (ImageView) convertView
					.findViewById(R.id.iv_image);
			holder.tv_title = (TextView) convertView
					.findViewById(R.id.tv_title);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		final CarouselInfo4 info = mLstData.get(position % mLstData.size());
		
		if(!TextUtils.isEmpty(info.getTitle())){
			holder.tv_title.setText(info.getTitle());
		}else{
			holder.tv_title.setText("");
		}
		if(info.getImageUrl() != null && info.getImageUrl().size()>0){
			ImageLoader.getInstance().displayImage(info.getImageUrl().get(0),
					holder.imgView, Constant.IMAGE_OPTIONS_FOR_ADVERTISEMENT, null);
		}
		
		holder.imgView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!Helper.checkConnection(mContext)) {
					Toast.makeText(mContext, R.string.net_no_notclick,
							Toast.LENGTH_SHORT).show();
					return;
				}
				if (Constant.VIDEO_TYPE_8.equals(info.getType())) {
					RequestEngine request = new RequestEngine();
					request.sendADVLogToServer(mContext,
							Constant.STATE_ADV_CZBM_1 + "",
							Constant.STATE_ADV_CZSX_4 + "_" + info.getId(), ""
									+ Constant.STATE_ADV_CZJG_3);
					new NetEngine().AdUpToServer(mContext, info.getAdid(),
							LoginInfo.getInstance().getUserId(), "7", "2", "-1");
				}
				TransferUtils.getInstance().transferActivity(mContext, info.getAssociation(),info.getType(),info.getTitle(),info.getAssociation(),Constant.ADV_SOURCE_OTHER,"");
			}
		});
		return convertView;
	}

	private class Holder {
		private ImageView imgView;
		private TextView tv_title;
	}

}
